﻿using System;
using System.Windows;
using System.Windows.Controls;
using compshop.Modules.ReceiveGoods.Presenter;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.ReceiveGoods.Views
{
    /// <summary>
    /// Interaction logic for ReceiveGoodsView.xaml
    /// </summary>
    public partial class ReceiveGoodsView : UserControl, IViewReceiveGoods
    {
        public ReceiveGoodsPresenter Presenter
        {
            get
            {
                return DataContext as ReceiveGoodsPresenter;
            }
            set
            {
                DataContext = value;

                if (Presenter.generalProduct != null)
                {
                    lblCode.Content = Presenter.generalProduct.GeneralProductID;
                    lblShipDate.Content = DateTime.Now;
                }
            }
        }
               
        public ReceiveGoodsView()
        {
            InitializeComponent();
        }

        public ReceiveGoodsView(ReceiveGoodsPresenter presenter)
            : this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (lblCode.Content is string)
            {
                if (string.IsNullOrEmpty((string)lblCode.Content))
                {
                    MessageBox.Show("Выберите продукт.");
                    return;
                }
            }

            if (!string.IsNullOrEmpty(tboxPPrice.Text) && !string.IsNullOrEmpty(tboxQuantity.Text))
            {
                decimal price;
                if (!decimal.TryParse(tboxPPrice.Text, out price))
                {
                    MessageBox.Show(Resource.NoCorrectPriceValue);
                    return;
                }

                int count;
                if (!int.TryParse(tboxQuantity.Text, out count))
                {
                    MessageBox.Show(Resource.NoCorrectCountProducts);
                    return;
                }

                if (count == 0)
                {
                    MessageBox.Show(Resource.NoCorrectCountProducts);
                    return;
                }

                Product product = new Product();
                product.GeneralProductID = (int)lblCode.Content;
                product.DepartmentID = ((Department)cboxDep.SelectedItem).DepartmentID;
                product.ShipDate =(DateTime) lblShipDate.Content;
                product.Status = 1;
                product.PurchasePrice = price;

                Presenter.AddProductToRestorePublisher(product, count);
                tboxQuantity.Clear();
                tboxPPrice.Clear();
            }
            else
            {
                MessageBox.Show(Resource.NoFillFileds);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            cboxDep.ItemsSource = Presenter.DepartmentList;
            cboxDep.DisplayMemberPath = "Name";
            cboxDep.SelectedIndex = 0;
            cboxDep.IsEnabled = false;
        }
    }
}
